<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;

class Option extends Base
{
    // 选项类型列表接口
    public function optionTypeList()
    {
        $data = input();//token

        $settingList = db('crm_setting_type')->select();
        $count = db('crm_setting_type')->count();
        $datas = array(
            'settingList'=>$settingList,
            'count'=>$count
        );
        apiReturn(200,'获取选项类型列表接口成功',$datas);
    }
    // 新增选项类型接口
    public function optionTypeAdd()
    {
        $data = input();//token name desc

        if (empty($data['name'])) {
            apiReturn(201,'选项类型不能为空');
        }
        $datas = array(
            'name'=>$data['name'],
            'desc'=>$data['desc'],
            'date'=>time()
        );

        db('crm_setting_type')->insert($datas);
        apiReturn(200,'新增选项类型成功');
    }
    // 获取选项类型接口
    public function optionTypeInfo()
    {
        $data = input();//token id

        $optionTypeInfo = db('crm_setting_type')->find($data['id']);
        apiReturn(200,'获取选项类型成功',$optionTypeInfo);
    }
    // 修改选项类型设置
    public function optionTypeEdit()
    {
        $data = input();//token id name desc

        if (empty($data['name'])) {
            apiReturn(201,'选项类型不能为空');
        }
        $datas = array(
            'name'=>$data['name'],
            'desc'=>$data['desc']
        );

        db('crm_setting_type')->where('id',$data['id'])->update($datas);
        apiReturn(200,'修改选项设置成功');
    }
    // 删除选项类型设置
    public function optionTypeDelete()
    {
        $data = input();//token id

        db('crm_setting_type')->delete($data['id']);
        apiReturn(200,'删除选项设置成功');
    }
    // 选项列表接口
    public function optionList()
    {
        $data = input();//token type

        $setting = db('crm_setting_type')->find($data['type']);
        $settingList = db('crm_setting')->where(array('type'=>$data['type']))->select();
        foreach ($settingList as $key => $value) {
            $settingList[$key]['status'] = ''.$value['status'].'';
        }

        $datas = array(
            'setting_type'=>$setting,
            'settingList'=>$settingList
        );
        apiReturn(200,'获取选项列表成功',$datas);
    }
    // 选项列表状态
    public function optionStatus()
    {
        $data = input();//token id

        $setting = db('crm_setting')->find($data['id']);
        if ($setting['status'] == 1) {
            $datas['status'] = 0;
        }else{
            $datas['status'] = 1;
        }
        db('crm_setting')->where(array('id'=>$data['id']))->update($datas);
        apiReturn(200,'修改状态成功');
    }
    // 添加选项设置
    public function optionAdd()
    {
        $data = input();//token type name desc

        $datas = array(
            'type'=>$data['type'],
            'name'=>$data['name'],
            'desc'=>$data['desc'],
            'status'=>$data['status'],
            'date'=>time()
        );
        db('crm_setting')->insert($datas);
        apiReturn(200,'添加成功');
    }
    // 获取选项信息接口
    public function optionInfo()
    {
        $data = input();//token id

        $optionTypeInfo = db('crm_setting')->find($data['id']);
        apiReturn(200,'获取选项信息成功',$optionTypeInfo);
    }
    // 修改选项设置
    public function optionEdit()
    {
        $data = input();//token id name desc status

        $datas = array(
            'name'=>$data['name'],
            'desc'=>$data['desc'],
            'status'=>$data['status']
        );

        db('crm_setting')->where('id',$data['id'])->update($datas);
        apiReturn(200,'修改选项成功');
    }
    // 删除选项设置
    public function optionDelete()
    {
        $data = input();//token id

        db('crm_setting')->delete($data['id']);
        apiReturn(200,'删除选项成功');
    }

    // 省市区列表
    public function allAreaList()
    {
        $data = input();//token parent_id
        if (!isset($data['parent_id'])) {
            apiReturn(201,'父级id为空');
        }
        $areaList = db('crm_region')->field('region_id,region_name,region_type')->where('parent_id',$data['parent_id'])->select();
        apiReturn(200,'获取省列表成功',$areaList);
    }

    // 地区列表
    public function areaList()
    {
        $data = input();//token province_id city_id area_id rows page

        $where = [];
        if (isset($data['province_id'])) {
            if (!empty($data['province_id'])) {
                $where['parent_id'] = $data['province_id'];
            }
        }
        if (isset($data['city_id'])) {
            if (!empty($data['city_id'])) {
                $where['parent_id'] = $data['city_id'];
            }
        }
        if (isset($data['area_id'])) {
            if (!empty($data['area_id'])) {
                $where['parent_id'] = $data['area_id'];
            }
        }

        $areaList = db('crm_region')->field('region_id,region_name,region_type')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->select();
        $count = db('crm_region')->where($where)->count();

        $datas = array(
            'areaList'=>$areaList,
            'count'=>$count
        );
        apiReturn(200,'获取地区列表成功',$datas);
    }

    // 操作记录列表
    public function operationList()
    {
        $data = input();//token rows page

        $where = [];
        if (isset($data['type'])) {
            if (!empty($data['type'])) {
                $where['type'] = $data['type'];
            }
        }
        if (isset($data['manage_id'])) {
            if (!empty($data['manage_id'])) {
                $where['manage_id'] = $data['manage_id'];
            }
        }
        if (isset($data['information_id'])) {
            if (!empty($data['information_id'])) {
                $where['information_id'] = $data['information_id'];
            }
        }
        if (isset($data['content'])) {
            if (!empty($data['content'])) {
                $where['content'] = ['like','%'.$data['content'].'%'];
            }
        }

        $operationList = db('crm_manage_operation')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('date DESC')->select();
        $count = db('crm_manage_operation')->where($where)->count();

        $datas = array(
            'operationList'=>$operationList,
            'count'=>$count
        );
        apiReturn(200,'获取操作列表成功',$datas);
    }

}
